package com.mis.pos.repository;

import com.mis.pos.entity.SaleEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.Id;
import javax.transaction.Transactional;
import java.util.List;

/**
 * @Description 订单DAO
 * @Author yuxiang
 * @CreatedDate 2019/11/14 15:52
 */
@Repository
public interface SaleRepository extends JpaRepository<SaleEntity,Integer>, JpaSpecificationExecutor<SaleEntity> {


    /**
     * 更改订单支付状态
     * @param saleId
     */
//    @Query("update SaleEntity st set st.isPayed = (byte)1 where st.id =:saleId") @Param("saleId")
    @Query(value = "update sale st set st.is_payed = 1 where st.id  =?1",nativeQuery = true)
    @Transactional
    @Modifying
    void updateSaled(Long saleId);


    /**
     * 根据id查询订单实体
     * @param id
     * @return
     */
    SaleEntity findSaleEntityById(@Param("id")Long id);


    /**
     * 分页查询订单实体
     * @param pageable
     * @return
     */
    Page<SaleEntity> findAll(Pageable pageable);


    /**
     * 分页查询订单列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Query(value = "select * from sale  st order by st.is_payed asc,st.update_time desc limit :pageNumber,:pageSize ",nativeQuery = true)
    List<SaleEntity> listSale(@Param("pageNumber") Integer pageNumber,@Param("pageSize") Integer pageSize);

    /**
     * 删除订单记录
     * @param id
     * @return
     */
    @Transactional
    @Modifying
    int deleteSaleEntityById(Long id);
}
